export default {
  async httpFetch(url) {
    // 一个对fetch 封装的示例
    return new Promise((resolve, reject) => {
      fetch(url, {
        method: "GET",
        timeout: 130,
        headers: { "content-type": "application/x-www-form-urlencoded" }
      })
        .then(e => e.json())
        .then(e => {
          resolve(e);
        });
    });
  },
  joinParmas(obj) {
    // 封装参数串
    return Object.keys(obj).map(e => `${e}=${obj[e]}`).join("&");
  },
  getParmas() {
    // 获取参数对象
    return Object.fromEntries(
      new URLSearchParams(location.href.replace("?", "_=&"))
    );
  },
  formatTime(v, s, e) {
    // + 8 * 3600 * 1000;
    console.log("formatTime", v);
    return new Date(v).toISOString().replace("T", " ").substring(s, e);
  },
  throttle(fun, delay) {
    // 节流
    let valid = true;
    return function() {
      let context = this;
      let args = arguments;
      if (!valid) {
        return;
      }
      valid = false;
      setTimeout(() => {
        fn.apply(context, args);
        valid = true;
      }, delay);
    };
  },
  ebounce(fn, delay) {
    // 防抖
    let timer = null; //借助闭包
    return function() {
      let context = this,
        args = arguments;
      timer && clearTimeout(timer);
      timer = setTimeout(function() {
        fn.apply(context, args);
      }, delay);
    };
  }
};
